var appPath = getAppPath();
var EDIT_IMG_IDX = 0;

$(function(){
	// 检索日期范围
	$("#queryDateStart").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	$("#queryDateEnd").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	
	// 初始化列表
	initPayhisGrid();
});

function dtFmt(dt){
	var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}
function dtParser(s){
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}

/**
 * 初始化加载 支付订单记录列表
 *	订单/发票 支付记录
 */
function initPayhisGrid(){
	$('#payHisGrid').datagrid({
	    url:appPath + "/itbpayhis/pageListPaymentHisInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'his_id',title:'支付记录ID',width:30,align:'center',sortable:false},
  	        {field:'type',title:'支付分类',width:30,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		var t = '<span style="color:red">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 10: t="商品订单"; break;
  	        			case 20: t="发票"; break;
  	        			case 30: t="购买模板"; break;
  	        			case 40: t="虚拟商品"; break;
  	        			case 50: t="秒杀订单"; break;
  	        		}
  	        		return t;
	        	}
		    },
  	        {field:'trade_obj_id',title:'交易对象ID',width:30,align:'center',sortable:false},
  	        {field:'trade_obj_no',title:'交易对象编号',width:35,align:'center',sortable:false},
	        {field:'state',title:'状态',width:30,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		//订单状态:10待支付、11已支付、12支付失败、20制作中、30配送中、40完成、50取消
  	        		var s = '<span style="color:#838B8B;">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 11: s='<span style="color:green">已支付</span>'; break;
  	        			case 12: s='<span style="color:red;">支付失败</span>'; break;
  	        			case 40: s='<span style="color:#00E5EE;">完成</span>'; break;
  	        		}
  	        		return s;
	        	}
		    },
		    {field:'pay_platform',title:'支付平台',width:30,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var s = '<span style="color:#838B8B;">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 10: s='<span style="color:green;">微信</span>'; break;
  	        			case 20: s='<span style="color:blue;">支付宝</span>'; break;
  	        			case 30: s='<span style="color:#FA8072;">苹果内购</span>'; break;
  	        		}
  	        		return s;
		    	}
		    },
		    {field:'edit',title:'详情',width:30,align:'center',sortable:false,
		    	formatter:function(value,rowData,rowIndex){
		    		return '<a href="javascript:void(0)" onclick="showPayInf('+rowData.his_id+')">支付详情</a>';
		    	}
		    },
		    {field:'desc',title:'记录描述',width:60,align:'center',sortable:false},
	        {field:'create_time',title:'创建时间',width:40,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:40,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[
			/*{
				text:'删除支付记录',
				iconCls:'icon-cancel',
				handler:function(){
					delOrder();
				}
			}*/
		]
	});
}


/*** 
	展示支付返回信息
	10表示微信支付返回信息
	30表示苹果内购支付返回字段信息
 */
function showPayInf(id){
	if(id && Number(id)>0){
		var url = appPath + "/itbpayhis/getPayReturnInfo.action?hisId="+id;
		$.get(url,function(ret){
			if(ret && ret.length>0){
				var info = ret[0];
				var htm = '';
				var pf = info.pay_platform;
				var arr = [];
				if(Number(pf)==10){//表示微信支付
					arr = ['无','return_code(返回状态码)','return_msg(返回信息)','appid(应用ID)','mch_id(商户号)','device_info(设备号)',
					           'nonce_str(随机字符串)','sign(签名)','result_code(业务结果)','err_code(错误代码)','err_code_des(错误代码描述)',
					           'openid(用户标识)','is_subscribe(是否关注公众账号)','trade_type(交易类型)','bank_type(付款银行)','total_fee(总金额)',
					           'fee_type(货币种类)','cash_fee(现金支付金额)','cash_fee_type(货币类型)','coupon_fee(代金券或立减优惠金额)',
					           'coupon_count(立减优惠使用数量)','transaction_id(微信支付订单号)','out_trade_no(商户订单号)','attach(商家数据包)',
					           'time_end(支付完成时间)'];
				}else if(Number(pf)==30){//苹果内购
					arr = ['无','state(返回状态码)','receipt_id(支付凭据base64编码)','product_id(支付的商品ID)','transaction_id(支付的交易ID)',
					           'quantity(数量)','original_transaction_id(返回结果)','purchase_date(返回结果)','purchase_date_ms(返回结果)',
					           'purchase_date_pst(返回结果)','original_purchase_date(返回结果)','original_purchase_date_ms(返回结果)','original_purchase_date_pst',
					           'is_trial_period(返回结果)','environment(环境"Sandbox")',
					           'receipt_type(支付凭据的类型)','adam_id(返回结果的receipt)','app_item_id(返回结果的receipt)','bundle_id(返回结果的receipt)',
					           'application_version(返回结果的receipt)','download_id(返回结果的receipt)','version_external_identifier(见返回结果的receipt)','receipt_creation_date(见返回结果的receipt)',
					           'request_date(见返回结果的receipt)','original_purchase_date(见返回结果的receipt)','original_application_version(见返回结果的receipt)'];
				}
				for(var i=1;i<arr.length;i++){
					var pay = "pay"+i
					var val = arr[i];
					var Pay_info = "Pay_info"+i;
					htm += '<tr><td class="queryTitle" width="100px">'+Pay_info+'</td><td class="queryContent" style="color:green;" width="250px">'+val+'</td>'+
					'<td class="queryContent" style="color:blue" width="250" id="Pay_info'+i+'"></td></tr>';
				}
				$("#weiTable").html(htm);
				$("#Pay_info1").html(info.pay_info1);
				if(Number(pf)==10){
					$("#Pay_info2").html(info.pay_info2);
				}else if(Number(pf)==30){
					var pay2 = info.pay_info2;
					$("#Pay_info2").html(pay2.substring(0,15)+"<span style='color:darkgrey'> ...... </span>"+pay2.substring(pay2.length-15,pay2.length));//字段太长
				}
				$("#Pay_info3").html(info.pay_info3);
				$("#Pay_info4").html(info.pay_info4);
				$("#Pay_info5").html(info.pay_info5);
				$("#Pay_info6").html(info.pay_info6);
				$("#Pay_info7").html(info.pay_info7);
				$("#Pay_info8").html(info.pay_info8);
				$("#Pay_info9").html(info.pay_info9);
				$("#Pay_info10").html(info.pay_info10);
				$("#Pay_info11").html(info.pay_info11);
				$("#Pay_info12").html(info.pay_info12);
				$("#Pay_info13").html(info.pay_info13);
				$("#Pay_info14").html(info.pay_info14);
				$("#Pay_info15").html(info.pay_info15);
				$("#Pay_info16").html(info.pay_info16);
				$("#Pay_info17").html(info.pay_info17);
				$("#Pay_info18").html(info.pay_info18);
				$("#Pay_info19").html(info.pay_info19);
				$("#Pay_info20").html(info.pay_info20);
				$("#Pay_info21").html(info.pay_info21);
				$("#Pay_info22").html(info.pay_info22);
				$("#Pay_info23").html(info.pay_info23);
				$("#Pay_info24").html(info.pay_info24);
			}
		});
		$('#weiPayDlg').window({
			title:'支付记录返回信息',
			iconCls:'icon-edit',
			modal: true,
			shadow: true,
			collapsible:false,
			minimizable:false,
			maximizable:false
		});
		$('#weiPayDlg').window('move',{top:20,left:20});
		$('#weiPayDlg').window('open');
	}else{
		$.messager.alert('提示信息','出错,请联系工程师','info');
	}
}


function getQueryParam(){
	var queryObject = {};
	var findState = $("#queryState").combobox('getValue');
	var findPayPf= $("#queryPayPf").combobox('getValue');
	var findTmplId = $("#queryTmplId").val();
	var queryObjId = $("#queryObjId").val();
	var findHisId = $("#queryHisId").val();
	var findDtS = $("#queryDateStart").datebox('getValue');
	var findDtE = $("#queryDateEnd").datebox('getValue');
	if(findTmplId) queryObject.findTmplId = findTmplId;//交易对象编号
	if(queryObjId && Number(queryObjId||'0')>0) queryObject.findObjId = queryObjId;//交易对象ID
	if(findHisId && Number(findHisId||'0')>0) queryObject.findHisId = findHisId;
	if(findState && Number(findState||'0')>0) queryObject.findType = findState;// 支付东西分类
	if(findPayPf && Number(findPayPf||'0')>0) queryObject.findPayPf = findPayPf;// 支付平台
	if(findDtS && findDtS.length>0) queryObject.findDateStart = findDtS+' 00:00:00';
	if(findDtE && findDtE.length>0) queryObject.findDateEnd = findDtE+' 23:59:59';
	return queryObject;
}


/**
 * 查询数据
 */
function doQuery(){
	var findDtS = $("#queryDateStart").datebox('getValue');
	var findDtE = $("#queryDateEnd").datebox('getValue');
	if(findDtS && findDtS.length>0){
		var chkRet = chkInputDate(findDtS);
		$("#queryDateStart").datebox('setValue',chkRet);
	}
	if(findDtE && findDtE.length>0){
		var chkRet = chkInputDate(findDtE);
		$("#queryDateEnd").datebox('setValue',chkRet);
	}
	
	$("#payHisGrid").datagrid('unselectAll');
	$("#payHisGrid").datagrid('load', getQueryParam());
}

// 判断日期格式[YYYY-MM-DD]
function chkInputDate(dt){
	var strDate=dt+'';
    var ss = (strDate.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        var chkDt = new Date(y,m-1,d);
		if((chkDt.getFullYear()==y) &&((chkDt.getMonth()+1)==m) &&(chkDt.getDate()==d)) {
        	return dtFmt(chkDt);
        }
    }
    return '';
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
